package com.people.netmon.reddit.monitor.service.impl;

import ai.people.netmon.framework.domain.common.monitor.MonitorUsersListVO;
import ai.people.netmon.framework.domain.common.monitor.MonitorUsersVO;
import ai.people.netmon.framework.domain.reddit.monitor.entity.RedditUsers;
import ai.people.netmon.framework.domain.reddit.monitor.request.QueryRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.people.netmon.reddit.monitor.mapper.RedditUsersMapper;
import com.people.netmon.reddit.monitor.service.RedditUsersService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhenglin
 * @since 2022-03-14
 */
@Service
public class RedditUsersServiceImpl extends ServiceImpl<RedditUsersMapper, RedditUsers> implements RedditUsersService {

    /**
     * 分页查询
     *
     * @param request 请求
     * @return {@link PageResponse}<{@link RedditUsers}>
     */
    @Override
    public PageResponse<MonitorUsersListVO> listPage(QueryRequest request) {
        Page<RedditUsers> page = new Page<>(request.getOriginPageNo(),request.getPageSize());
        LambdaQueryWrapper<RedditUsers> wrapper = request.createWrapper();
        Page<RedditUsers> result = page(page, wrapper);
        List<RedditUsers> records = result.getRecords();
        List<MonitorUsersListVO> finalResult = new ArrayList<>();
        records.forEach(x->finalResult.add(new MonitorUsersListVO(x)));
        return PageResponse.builder(finalResult,result.getTotal(),result.getPages(),result.getCurrent());
    }

    /**
     * 通过username查询用户信息
     *
     * @param userName 用户名
     * @return {@link RedditUsers}
     */
    @Override
    public MonitorUsersVO getUsers(String userName) {
        RedditUsers byId = getById(userName);
        if (byId == null){
            MonitorUsersVO monitorUsersVO = new MonitorUsersVO();
            monitorUsersVO.setUsername(userName);
            return monitorUsersVO;
        }
        return new MonitorUsersVO(byId);
    }
}
